package com.hulk.ratel.manage.commoncache;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hulk.common.util.BeanMapper;
import com.hulk.ratel.common.constant.enums.ErrFlag;
import com.hulk.ratel.common.constant.enums.UseFlag;
import com.hulk.ratel.manage.commonutil.bo.ErrcodeConvertBO;
import com.hulk.ratel.persistence.entity.TErrcodeConvert;
import com.hulk.ratel.persistence.repository.TErrcodeConvertRepository;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.List;
import java.util.Optional;

/**
 * Created by hulk on 2017/11/22.
 * 【参数信息】机构合作方应答码转换表
 */
@Slf4j
@Component
public class ErrCodeConvertCache {

    @Autowired
    private TErrcodeConvertRepository repository;

    /**
     * 查询机构合作方应答码转换
     *
     * @param workId
     * @param upOrgCode
     * @param respCode
     * @return
     */
    @Cached(key = "#upOrgCode + #respCode + 'ErrCodeConvertCache_findErrCodeConvert'", cacheType = CacheType.LOCAL ,localLimit = 4096, localExpire =600)
    public ErrcodeConvertBO findErrCodeConvert(String workId, @javax.annotation.Nonnull String upOrgCode, @javax.annotation.Nonnull String respCode) {


        TErrcodeConvert  convert =  new TErrcodeConvert();
        convert.setRespCode(respCode);
        convert.setOrgCode(upOrgCode);
        convert.setUseFlag(UseFlag.Y.getValue());
        convert.setErrFlag(ErrFlag.Y.getValue());
        EntityWrapper<TErrcodeConvert> query = new EntityWrapper<>(convert);
        Optional<List<TErrcodeConvert> > opt = Optional.ofNullable(repository.selectList(query));

        if(opt.isPresent()){
           convert  =opt.get().get(0) ;
            ErrcodeConvertBO pbo = new ErrcodeConvertBO();
            BeanMapper.copyNotNull(convert,pbo);
            return pbo;
        }else{
            return null;
        }


    }
}
